{% extends 'frame.html' %}

{% block right-container %}
    <div class="right-container">
        <!-- 项目基本信息 -->
        <div class="panel panel-default">
            <div class="panel-heading">
                项目信息
            </div>
            <div class="panel-body">
                <p><span>项目名：</span>{{ project.ProJectName }}</p>
                <p><span>包含主机：</span>{{ project.IpAddress }}</p>
                <p><span>仓库地址：</span><a href="http://{{ project.GitAddress }}{{ project.GitRepo }}" target="_blank" class="GitRepo" name={{ project.GitRepo }}> http://{{ project.GitAddress }}{{ project.GitRepo }}</a></p>
                <p><span>服务器端路径：</span>{{ project.ProPath }}</p>
                <p>
                    <span>更新时间：</span>
                    <span class="Updatetime">
                        <script>
                                    document.write(new Date("{{ project.UpdateTime }}" * 1000).toLocaleString())
{#                                    {{ project.UpdateTime }}#}
                                </script>
                    </span>
                </p>
            </div>
        </div>
        <!-- 全部更新按钮 -->
        <div class="panel panel-success">
            <div class="panel-heading">
                全部更新
            </div>
        <!-- git pull 更新按钮 -->
            <div class="panel-body">
                <button type="button" class="btn btn-danger all-pull" value="">全部更新</button>
                <span class="RollBack"></span>
{#                <textarea class="RollBack"></textarea>#}
            </div>
        </div>

        <!-- 更新指定按钮 -->
        <div class="panel panel-info">
            <div class="panel-heading form-group">
                更新指定文件
            </div>
            <div class="panel-body">
                <div class="row fileinput">
                    <div class="input-group col-md-8" style="float: left">
                        <span class="input-group-addon" id="basic-url">http://{{ project.GitAddress }}{{ project.GitRepo }}/</span>
                        <input type="text" class="form-control filename" id="basic-url" name="" aria-describedby="basic-addon1">
                    </div>
                    <div class="filebutton col-md-4">
                        <button class="btn btn-danger buttons-unit fileadd">添加文件</button>
                        <button class="btn btn-primary buttons-unit openfile" disabled="disabled">查看</button>
                        <button class="btn btn-primary buttons-unit choice">从列表中选择</button>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-heading">
                        已选文件 (PS:点击取消选择)
                    </div>
                    <div class="panel-body choicefile">
                        <div class="addedfile"></div>
                        <button class="btn btn-danger updatefile">更新选择文件</button>
                        <span class="FileUpdateBack"></span>
                    </div>
                </div>
            </div>
        </div>

        <!-- 文件列表弹窗 -->
            <div class="panel panel-group panel-danger filelistdiv" style="display: none">
                <div class="panel-heading">文件列表</div>
                <div class="panel-body file-panel-body">
                    <div class="filelistbody">
                        <ul class="list-group filelist">
                        </ul>
                    </div>
                    <button class="btn btn-success choiceover">完成</button>
                </div>
            </div>

        <!-- 更新结果弹窗 -->
        <div class="execResult panel panel-success">
            <div class="panel-heading">
                更新结果
            </div>
            <div class="panel-body">
                <div class="resultdiv">
                    <table class="table table-hover table-condensed resultTable">
                        <tr>
                            <th class="success">主机</th>
                            <th class="success">执行结果</th>
                            <th class="success">Success</th>
                        </tr>
                    </table>
                </div>
                <button class="btn btn-info knowleage">确定</button>
            </div>
        </div>
    </div>

{% endblock %}

{% block customjs %}
    <script type="text/javascript" src="/static/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".all-pull").click(function () {
                $(".all-pull").text('更新中···');
                $(".all-pull").attr({
                    "disabled": "disabled"
                });
                alert("全部更新");
                $.ajax({
                    url: '/AllUpdate/',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        'proid': {{ project.id }}
                    },
                    success: function (obj) {
                        $(".execResult").css("display","block");
                        {#console.log(obj);#}
                        for (var k in obj){
                            var html = '<tr class="resu"><td class="serverName">'+k+'</td><td class="saltReturn">'+obj[k][0].replace(/\\n/gm,'<br>')+'</td><td class="successNum">'+obj[k][1]+'</td></tr>';
                            $(".resultTable").append(html);
                        }
                        $(".all-pull").removeAttr("disabled");
                        $(".all-pull").text('全部更新');
                    },
                    error: function (result) {
                        {#alert(result);#}
                        alert("all update error");
                    }
                });
            });
            $(".fileadd").click(function () {
                var fileName = $(".filename").val();
                var addHtml= '<button class="btn btn-default fileunit">' + fileName + '</button>';
                if (fileName.length > 0) {
                    $(".addedfile").append(addHtml);
                }
                $(".filename").val("");
                $(".fileunit").click(function () {
                    $(this).remove();
                });
            });
            $(".openfile").click(function () {
                {#console.log("openfile button");#}
            });
            $(".updatefile").click(function () {
                $(".updatefile").text('更新中···');
                $(".updatefile").attr({
                    "disabled": "disabled"
                });
                var length = $(".fileunit").length;
                var fileNames = [];
                for(var i = 0; i < length; i++){
                    {#console.log($(".fileunit").eq(i).text());#}
                    fileNames.push($(".fileunit").eq(i).text());
                }
                {#console.log(fileNames);#}
                {#alert(fileNames.join(','));#}
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: '/FileUpdate/',
                    data: {
                        "test":"txt",
                        "Proid": {{ project.id }},
                        "Files": fileNames.join(',')
                    },
                    success: function(obj) {
                        $(".execResult").css("display","block");
                        $(".updatefile").text('更新选定文件');
                        for (var k in obj){
                            {#console.log(k);#}
                            var html = '<tr class="resu"><th class="serverName">'+k+'</th><th class="saltReturn">'+obj[k][0]+'</th><th class="successNum">'+obj[k][1]+'</th></tr>';
                            {#console.log(obj[k]);#}
                            $(".resultTable").append(html);
                        }
                        result = JSON.stringify(obj);
                        $(".updatefile").removeAttr("disabled");
                        alert('更新完成');
                    },
                    error: function(obj) {
                        result = JSON.stringify(obj);
                        $(".FileUpdateBack").text(result);
                        alert('更新失败');
                    }
                })
            });
            $(".knowleage").click(function () {
                $(".execResult").css("display","none");
                $("tr").remove(".resu");
            });

            $(".choice").click(function () {
                $(".fileLi").remove();
                var ProName = $(".GitRepo").prop('name').split('/').pop().split('.').slice(0,-1);
                {#console.log(ProName);#}
                {#console.log(ProName.length);#}
                if (ProName.length > 1){
                    ProName = ProName.join('.');
                }
                {#console.log(ProName);#}
                var CurPath = "";
                function UpPath(path) {
                    $.ajax({
                        url: '/getfilelist/',
                        dataType: 'json',
                        data: { 'FilePath': path, 'repository':ProName },
                        type: 'get',
                        success: function (result) {
                            $(".fileLi").remove();
                            CurPath = path;
                            LoadList(result);
                            {#console.log(CurPath);#}
                        },
                        error: function (result) {
                            {#alert(result);#}
                            {#console.log("error")#}
                        }
                    });
                }
                function LoadList(Data) {
                    if (CurPath == '') {
                        {#console.log("curpath is null");#}
                    }
                    else {
                        $(".filelist").append("<li class='list-group-item list-group-item-success updirectory fileLi'><span class='uppath'>返回上级</span></li>");
                        {#console.log("curpath is not null");#}
                    }
                    for (var m=0;m<Data.length;m++) {
                        {#console.log(Data);#}
                        if (Data[m].type == 'tree') {
                            {#console.log(222);#}
                            $(".filelist").append("<li class=\"list-group-item list-group-item-success directory fileLi\">" +
                                "<span><a class=\"expanding btn\" style='text-decoration:none;out-line:none;color:none'>" + Data[m].name + "</a></span>" +
                                "<span class=\"addfile\" style=\"float:right;cursor: pointer\">" +
                                "添加" +
                                "</span>" +
                                "</li>")
                        }
                        else if (Data[m].type == 'blob') {
                            {#console.log(333);#}
                            $(".filelist").append('<li class="list-group-item list-group-item-danger file fileLi">' +
                                "<span><a class='isfile' style='text-decoration:none;out-line:none;color:none'>" +
                                Data[m].name + "</a></span>" +
                                "<span class='addfile' style='float:right;cursor:pointer'>添加<\span>" +'</li>')
                        }
                    }
                    $(".expanding").click(function () {
                        {#console.log($(this).text());#}
                        FilePath = $(this).text();
                        if (CurPath == "") {
                            CurPath += FilePath;
                        }
                        else {
                            CurPath += '/' + FilePath;
                        }
                        $.ajax({
                            url: '/getfilelist/',
                            dataType: 'json',
                            type: 'GET',
                            data: {
                                FilePath: CurPath,'repository':ProName
                            },
                            success: function (result) {
                                $(".fileLi").remove();
                                LoadList(result);
                            },
                            error: function (result) {
                                {#console.log(result);#}
                            }
                        });
                    });
                    $(".uppath").click(function(){
                    {#console.log("click");#}
                    var UpPath1 = CurPath.split('/');
                    {#console.log(UpPath1.length);#}
                    if (UpPath1.length == 2){
                        {#console.log("Up path is 1<n<2");#}
                        var UpPath2 = UpPath1[0];
                        UpPath(UpPath2);
                    }
                    else if (UpPath1.length == 1){
                        var UpPath2 = '';
                        {#console.log("Up path is 1");#}
                        UpPath(UpPath2);
                    }
                    else{
                        for (i=0;i<UpPath1.length-2;i++){
                            {#console.log("Up path is more then 3");#}
                            var UpPath2 = UpPath1[i]+'/'+UpPath1[i+1];
                            UpPath(UpPath2);
                        }
                    }
                });
                    $(".addfile").click(function () {
                        var Name = $(this).parent().find("span").eq(0).find("a").text();
                        var length = $(".fileunit").length;
                        {#console.log('length is '+length);#}
                        var fileNames = [];
                        if(CurPath == ''){
                            AllPath = Name;
                            {#console.log(AllPath);#}
                        }
                        else{
                            AllPath = CurPath + '/' + Name;
                            {#console.log(AllPath);#}
                        }
                        var addHtml= '<button class="btn btn-default fileunit">' + AllPath + '</button>';
                        if(length == '0'){
                            if (AllPath.length > 0) {
                                $(".addedfile").append(addHtml);
                            }
                        }
                        else {
                            for(var i = 0; i < length; i++){
                                {#console.log($(".fileunit").eq(i).text());#}
                                fileNames.push($(".fileunit").eq(i).text());
                            }
                            if($.inArray(AllPath,fileNames)){
                                {#console.log("all path is not in");#}
                                $(".addedfile").append(addHtml);
                            }
                            else{
                                alert("已经添加,不要重复添加");
                                {#console.log("allpath is in filenames");#}
                            }
                            {#console.log(fileNames);#}
                        }

                        {#$(".filename").val("");#}
                        $(".fileunit").click(function () {
                            $(this).remove();
                        });

                    })
            }
            $.ajax({
                url: '/getfilelist/',
                dataType: 'json',
                type: 'GET',
                data: {
                    FilePath: CurPath , 'repository':ProName
                },
                success: function (result){
                    LoadList(result);
                    {#console.log("loadlist result over");#}
                    $(".filelistdiv").css('display','block');
                },
                error: function (result) {
                    {#console.log(result);#}
                }
            });
        });
            $(".choiceover").click(function () {
                $(".filelistdiv").css("display",'none');
            })
    })
    </script>
{% endblock %}